<?php


namespace app\app\controller;


use app\helper\SubjectHelper;
use app\model\AdminSubject;
use app\util\ReturnCode;
use think\Db;
use think\exception\DbException;

class Subject extends Base
{    /**
 * 获取列表
 * @return array
 * @author chniccs
 */
    public function getList()
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $subId = $this->request->get('sub_id', null);
        try {
            $model = AdminSubject::get($subId,null,true);
            if(!$model){
                return $this->buildFailed(ReturnCode::INVALID, '不存在的专题');
            }
            $where = ['sub_active.sub_id' => $subId, 'sub_active.tid' => $model->tid];
            $sort = $model->sort;
            $sort = SubjectHelper::getOrderBySort($sort);
            $subsql = Db::table('admin_active_sku sku')->alias('sku')
                ->field('min(lowest_price) as price,sum(sales + init_sales) as sales,vip_discount,aid')
                ->group('aid')
                ->buildSql();
            $listObj = Db::table('admin_subject_active')->alias('sub_active')
                ->where($where)
                ->field('sub_active.sub_id as sub_id,sub_active.tid as tid')
                ->join('admin_actives actives', 'sub_active.goods_id = actives.id')
                ->field('actives.create_time as create_time,actives.id as goods_id,actives.banners as banner,actives.title as title,actives.status as status')
                ->join([$subsql => 'sku'], 'actives.id = sku.aid')
                ->field('sku.price as price,sku.sales as sales,vip_discount')
                ->order($sort)
                ->paginate($limit, false, ['page' => $start])
                ->toArray();
            return $this->buildSuccess([
                'sub_data' => $model->getData(),
                'list' => $listObj['data'],
                'count' => $listObj['total']
            ]);
        } catch (DbException $e) {
        };
        return $this->buildFailed(ReturnCode::INVALID, '获取失败');
    }

}